<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程管理</title>
<link rel="stylesheet" href="/layui/css/layui.css"></link>
<link rel="stylesheet" href="/css/css.css"></link>
<style>
	.layui-table-body .layui-table-cell{
		height:50px;
		line-height: 50px;
	}
</style>
</head>
<body>

<div style="padding: 20px">
	        <span class="layui-breadcrumb">
			  <a>首页</a>
			  <a><cite>课程管理</cite></a>
			</span>
	<div style="padding-top: 20px;">
		<div style="padding: 20px">
			<div class="layui-form">

				<div class="layui-inline">
					<label class="layui-form-label">课程名称：</label>
					<div class="layui-input-inline">
						<input type="text" id="s_name"  placeholder="请输入" autocomplete="off" style="width: 400px" class="layui-input" onkeydown="if(event.keyCode==13) search()"/>
					</div>
				</div>


				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-list"  lay-submit="" onclick="search()" lay-filter="LAY-app-contlist-search">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn" ></i>
					</button>
				</div>
			</div>
		</div>
		<div>
			<div>
				<button class="layui-btn layuiadmin-btn-list" data-type="batchdel" onclick="addCourse()">发布免费课程</button>
				<button class="layui-btn layuiadmin-btn-list layui-btn-danger" data-type="batchdel" onclick="addVIPCourse()">发布VIP课程</button>
			</div>
		</div>
		<div>
			<table width="100%" id="courseListTable" ></table>
		</div>
	</div>
</div>

<script src="/layui/layui.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/common.js"></script>
<script type="text/javascript">

    layui.use(['element','form','table'], function(){
        var form=layui.form;
        var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
        $ = layui.jquery; // 使用jquery
        table = layui.table;

        table.render({
            elem: '#courseListTable'
            ,url:'/admin/course/list'
            ,cols: [[
                {type:'checkbox'}
                ,{field:'id', width:60,title: '编号'}
                ,{field:'imageName', width:200,title: '课程图片',templet:formatImageName}
                ,{field:'name', title: '课程名称',templet:formatName}
                ,{field:'type', width:100,title: '课程类型',templet:formatType}
                ,{field:'view', width:100,title: '学习次数'}
                ,{field:'courseType', width:150,title: '课程类别',templet:formatCourseType}
                ,{field:'teacher', width:100,title: '授课老师',templet:formatTeacher}
                ,{field:'action', width:300, title: '操作',align:'center',templet:formatAction}
            ]]
            ,page: true
        });

    });


    function search(){
        var s_name=$("#s_name").val();
        table.reload("courseListTable",{
            page:{
                curr:1
            }
            ,where:{
                name:s_name
            }
        })
    }

    function deleteOne(id){
        layer.confirm('您确定要删除这条记录吗？', {
            title:"系统提示"
            ,btn: ['确定','取消'] //按钮
        }, function(){
            layer.closeAll('dialog');
            $.post("/admin/course/delete",{"id":id},function(result){
                if(result.success){
                    layer.msg("删除成功！");
                    table.reload("courseListTable",{});
                }else{
                    layer.msg("删除失败，请联系管理员！");
                }
            },"json");
        }, function(){

        });
    }

    function addCourse(){
        window.parent.openPage('addFreeCourse.html');
    }

    function addVIPCourse(){
        window.parent.openPage('addVIPCourse.html');
    }

    function modifyCourse(id){
        layer.open({
            type: 2,
            title: '修改课程',
            area: ['1000px', '730px'],
            content: '/admin/saveCourse.html?id='+id //iframe的url
        });
    }

    function modifyCourseImage(id){
        layer.open({
            type: 2,
            title: '修改轮播帖子图片',
            area: ['500px', '500px'],
            content: '/admin/saveCourseImage.html?id='+id //iframe的url
        });
    }

    function formatAction(d){
        return "<button class='layui-btn layui-btn-normal layui-btn-xs' onclick='modifyCourseImage("+d.id+")'><i class='layui-icon layui-icon-edit'></i>修改图片</button><button class='layui-btn layui-btn-normal layui-btn-xs' onclick='modifyCourse("+d.id+")'><i class='layui-icon layui-icon-edit'></i>编辑</button><button class='layui-btn layui-btn-warm layui-btn-xs' onclick='deleteOne("+d.id+")'><i class='layui-icon layui-icon-delete' ></i>删除</button>";
    }

    function formatImageName(d) {
        return "<img  src='/courseImages/"+d.imageName+"'/>";
    }

    function formatCourseType(d){
        return d.courseType.name;
    }

    function formatTeacher(d){
        return d.teacher.nickName;
	}

    function formatType(d) {
        if(d.type=='free'){
            return "免费课程";
		}else{
            return "<font color='red'>VIP课程</font>";
		}
    }

    function formatName(d) {
        return "<a target='_blank' href='/course/"+d.id+"'>"+d.name+"</a>";
    }




</script>
</body>
</html>